package com.tomorrow.runner.activity.history;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tomorrow.runner.R;
import com.tomorrow.runner.data.model.ActivityModel;
import com.tomorrow.runner.util.WeatherUtil;

import cn.sucun.android.log.Log;
import cn.sucun.android.utils.DateUtils;

/**
 * Created by tomorrow on 16-5-7.
 */
public class HistoryAdapter extends CursorAdapter{

    public HistoryAdapter(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
    }

    @Override
    public Cursor swapCursor(Cursor newCursor) {
        initIndex(newCursor);
        return super.swapCursor(newCursor);
    }

    private int INDEX_OF_TYPE;
    private int INDEX_OF_START_TIME;
    private int INDEX_OF_END_TIME;
    private int INDEX_OF_WEATHER;
    private int INDEX_OF_POSITION;
    private void initIndex(Cursor newCursor) {
        INDEX_OF_TYPE = newCursor.getColumnIndexOrThrow(ActivityModel.ACTIVITY_TYPE);
        INDEX_OF_START_TIME = newCursor.getColumnIndexOrThrow(ActivityModel.START_TIME);
        INDEX_OF_END_TIME = newCursor.getColumnIndexOrThrow(ActivityModel.END_TIME);
        INDEX_OF_WEATHER = newCursor.getColumnIndexOrThrow(ActivityModel.WEATHER);
        INDEX_OF_POSITION = newCursor.getColumnIndexOrThrow(ActivityModel.POSITION);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = View.inflate(context, R.layout.history_list_item,null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.icon = (ImageView) view.findViewById(R.id.type_img);
        viewHolder.primary = (TextView) view.findViewById(R.id.primary_info);
        viewHolder.second = (TextView) view.findViewById(R.id.second_info);
        viewHolder.position = (TextView) view.findViewById(R.id.position_text);
        viewHolder.weather = (ImageView) view.findViewById(R.id.weather_icon);
        view.setTag(viewHolder);
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        int type = cursor.getInt(INDEX_OF_TYPE);
        if (type == ActivityModel.TYPE_RUN){
            viewHolder.icon.setImageResource(R.mipmap.run);
        }
        else{
            viewHolder.icon.setImageResource(R.mipmap.fit);
        }
        long start_time = cursor.getLong(INDEX_OF_START_TIME);
        Log.i("TIME",""+start_time);
        long end_time = cursor.getLong(INDEX_OF_END_TIME);
        viewHolder.primary.setText(DateUtils.formatDateString(start_time,false));
        viewHolder.second.setText(DateUtils.formatDateString(end_time,false));
        String position = cursor.getString(INDEX_OF_POSITION);
        viewHolder.position.setText(position);
        String weather = cursor.getString(INDEX_OF_WEATHER);
        viewHolder.weather.setImageResource(WeatherUtil.getWeatherIconId(weather));
    }

    class ViewHolder{
        public ImageView icon;
        public TextView primary;
        public TextView second;
        public TextView position;
        public ImageView weather;
    }
}
